package com.ttb.smshelper;

import java.util.ArrayList;
import java.util.List;

import com.ttb.database.Blocker;
import com.ttb.database.BlockerDAO;

import android.R.bool;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;

import android.widget.RadioGroup;

public class chanmessage extends Activity {
	EditText editvalue;
	Button btnAdd;
	ArrayList<list_chan> arrChan = new ArrayList<list_chan>();
	ArrayList<Blocker> blockList = new ArrayList<Blocker>();
	ArrayAdapter<Blocker> adapter = null;
	ListView lvblocklist;
	BlockerDAO blockDao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manhinh_chan);
		blockDao = new BlockerDAO(this.getApplicationContext());
		doFormWidgets();
		addEventFormWidgets();

	}

	private void addEventFormWidgets() {
		// TODO Auto-generated method stub
		btnAdd.setOnClickListener(new MyButtonEvent());
		lvblocklist.setOnItemLongClickListener(new MyListViewEvent());

	}

	private class MyButtonEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btnAdd:
				processAddJob();
				break;
			}

		}

		private void processAddJob() {
			String type = "";

			RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);

			int id = group.getCheckedRadioButtonId();

			RadioButton rad = (RadioButton) findViewById(id);

			type = rad.getText() + "";
			String value = editvalue.getText() + "";
			//list_chan chan = new list_chan(value, type);
			//arrChan.add(chan);
			Blocker blocker = new Blocker(value, type);
			blockDao.addBlocker(blocker);
			blockList.add(blocker);
			adapter.notifyDataSetChanged();

			editvalue.setText("");

			editvalue.requestFocus();
		}
		

	}

	private class MyListViewEvent implements OnItemLongClickListener {

		private int yesOrNo;
		
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				final int arg2, long arg3) {
			Log.d("arg2", String.valueOf(arg2));
			final AlertDialog.Builder b = new AlertDialog.Builder(chanmessage.this);
		        b.setIcon(android.R.drawable.ic_dialog_alert);
		        b.setTitle("Delete");
		        b.setMessage("Are you sure?");
		        b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
		                    yesOrNo = 1;
		                    Log.d("List size:", String.valueOf(blockList.size()));
		                    Blocker blocker = blockList.get(arg2);
		                    blockList.remove(blocker);
		                    blockDao.deleteBlocker(blocker.getId());
							adapter.notifyDataSetChanged();
		                }
		        });
		        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
		        

						public void onClick(DialogInterface dialog, int whichButton) {
		                    yesOrNo = 0;
		                }
		        });

		        b.show();
				return false;

		       
		    }

			
		}

	private void doFormWidgets() {
		// TODO Auto-generated method stub
		editvalue = (EditText) findViewById(R.id.editvalue);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		lvblocklist = (ListView) findViewById(R.id.lvblocklist);

		
		adapter = new ArrayAdapter<Blocker>(this,
				android.R.layout.simple_list_item_1, blockList);
		lvblocklist.setAdapter(adapter);
		//load data from db
		List<Blocker> listBlocker = blockDao.getAllBockers();
		for(Blocker blocker : listBlocker)
		{
			blockList.add(blocker);
		}
		adapter.notifyDataSetChanged();
	}

}
